using System;
using System.Net;
using FtpLib;
using Wintellect.PowerCollections;

namespace Quiki.Services
{
    public class FakeFtpConnection : IFtpConnection
    {
        public FakeFtpConnection(FakeFtpClient client)
        {
            this.client = client;
        }

        public void ChangeDirectory(string directory)
        {
            client.RecordAction("ChangeDirectory {0}", directory);

            if (false == DirectoryExists(directory))
                throw new FtpException((int) FtpStatusCode.ActionNotTakenFileUnavailable, null);
        }

        public void CreateDirectory(string directory)
        {
            client.RecordAction("CreateDirectory {0}", directory);
            existingDirectories.Add(directory);
        }

        public bool DirectoryExists(string directory)
        {
            client.RecordAction("DirectoryExists {0}", directory);
            return existingDirectories.Contains(directory);
        }

        public void PutFile(string localFileName, string remoteFileName)
        {
            client.RecordAction("PutFile {0} {1}", localFileName, remoteFileName);
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        protected virtual void Dispose(bool disposing)
        {
            if (false == disposed)
            {
                // clean native resources         

                if (disposing)
                {
                    // clean managed resources            
                }

                disposed = true;
            }
        }

        private readonly FakeFtpClient client;
        private bool disposed;
        private Set<string> existingDirectories = new Set<string>();
    }
}